package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FragmentUtils;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.SearchActivity;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.dialog.SetAnswerDialog;
import flc.ast.dialog.SetPasswordDialog;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private List<Fragment> mFragmentList;
    private LocalVideoFragment localVideoFragment;
    private VideoAlbumFragment videoAlbumFragment;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mFragmentList = new ArrayList<>();
        localVideoFragment = new LocalVideoFragment();
        mFragmentList.add(localVideoFragment);
        videoAlbumFragment = new VideoAlbumFragment();
        mFragmentList.add(videoAlbumFragment);
        FragmentUtils.add(getChildFragmentManager(), mFragmentList, R.id.fl_container, 0);

        mDataBinding.tvLocalVideo.setOnClickListener(this);
        mDataBinding.tvVideoAlbum.setOnClickListener(this);
        mDataBinding.ivLayout.setOnClickListener(this);
        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.ivAddAlbum.setOnClickListener(this);
        mDataBinding.ivPrivacyVideo.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivLayout://布局
                localVideoFragment.showRankDialog();
                break;
            case R.id.tvLocalVideo://本地视频
                clearSelection();
                mDataBinding.tvLocalVideo.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvLocalVideo.setTextSize(24);
                mDataBinding.ivLocalVideo.setVisibility(View.VISIBLE);
                FragmentUtils.showHide(0, mFragmentList);
                mDataBinding.llLocalVideoTool.setVisibility(View.VISIBLE);
                mDataBinding.llVideoAlbumTool.setVisibility(View.GONE);
                break;
            case R.id.tvVideoAlbum://视频相册
                clearSelection();
                mDataBinding.tvVideoAlbum.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvVideoAlbum.setTextSize(24);
                mDataBinding.ivVideoAlbum.setVisibility(View.VISIBLE);
                FragmentUtils.showHide(1, mFragmentList);
                mDataBinding.llLocalVideoTool.setVisibility(View.GONE);
                mDataBinding.llVideoAlbumTool.setVisibility(View.VISIBLE);
                break;
            case R.id.ivSearch://搜索
                SearchActivity.start(mContext, localVideoFragment.getHasLineStyle());
                break;
            case R.id.ivAddAlbum://添加视频相册
                videoAlbumFragment.showAddAlbumDialog();
                break;
            case R.id.ivPrivacyVideo://加密视频
                //是否完成设置密码
                boolean isSetPassword = SPUtil.getBoolean(getContext(), Content.isHavePassword, false);
                if (isSetPassword) {
                    passwordDialog(false, "", "");
                } else {
                    answerDialog(false);
                }
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void answerDialog(boolean isForget) {
        SetAnswerDialog answerDialog = new SetAnswerDialog(mContext);
        answerDialog.isForget = isForget;
        answerDialog.setListener(new SetAnswerDialog.Listener() {
            @Override
            public void setPassword(String question, String answer) {
                passwordDialog(true, question, answer);
            }
        });
        answerDialog.show();
    }

    private void passwordDialog(boolean isSetPassword, String question, String answer) {
        SetPasswordDialog passwordDialog = new SetPasswordDialog(mContext);
        passwordDialog.isFile = false;
        passwordDialog.isSetPassword = isSetPassword;
        if (isSetPassword) {
            passwordDialog.question = question;
            passwordDialog.answer = answer;
        }
        passwordDialog.setListener(new SetPasswordDialog.Listener() {
            @Override
            public void forgetPassword() {
                answerDialog(true);
            }
        });
        passwordDialog.show();
    }

    private void clearSelection() {
        mDataBinding.tvLocalVideo.setTextColor(Color.parseColor("#80FFFFFF"));
        mDataBinding.tvLocalVideo.setTextSize(15);
        mDataBinding.ivLocalVideo.setVisibility(View.GONE);
        mDataBinding.tvVideoAlbum.setTextColor(Color.parseColor("#80FFFFFF"));
        mDataBinding.tvVideoAlbum.setTextSize(15);
        mDataBinding.ivVideoAlbum.setVisibility(View.GONE);
    }

}